package com.msw.java;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class SqlUtils {

	private static Logger log = LoggerFactory.getLogger(SqlUtils.class);
	private static String url = "jdbc:mysql://192.168.32.101:3306?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false&amp;zeroDateTimeBehavior=convertToNull";

	static {
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static Connection getConnnection(String user,String password){
		try {
			return DriverManager.getConnection(SqlUtils.url, user, password);
		} catch (SQLException e) {
			System.out.println("Access denied for user (using password: NO)");
			System.out.println(e.getMessage());
			log.error(e.getMessage(),e);
			return null;
		}
	}
	
	public static void closeConnection(Connection conn) throws SQLException{
		if(conn!=null){
			conn.close();			
		}
	}
	
	public static ResultSet getResultSet(Connection conn ,String sql){
		try {
			Statement stat = conn.createStatement();
			return stat.executeQuery(sql);
		} catch (SQLException e) {
			log.error(e.getMessage(),e);
			return null;
		}
	}
}
